package com.app.serve.exception;

import com.app.serve.common.JsonResp;
import com.app.serve.enums.ErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 *  处理统一异常
 */
@ControllerAdvice
public class GlobalException {

    private final Logger log = LoggerFactory.getLogger(GlobalException.class);

    // 系统异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(Exception e) {
        log.error("Default Exception", e);
        return JsonResp.error(ErrorCodeEnum.SYSTEM_ERROR);
    }

    // 业务异常
    @ExceptionHandler(JsonRespException.class)
    @ResponseBody
    public Object handleException(JsonRespException e) {
        log.error("JsonRespException", e);
        return JsonResp.error(e.getCode(), e.getMsg());
    }
}
